<template>
    <!-- <div class="container"> -->
      <!-- <h1 class="logo"><RouterLink to="/">小兔鲜</RouterLink></h1> -->
      <ul class="navs">
        <li class="home"><RouterLink to="/">首页</RouterLink></li>
    <li v-for="item in list" :key="item.id"  @mouseenter="show(item)" @mouseleave="hide(item)" >
      <RouterLink :to="`/category/${item.id}`" @click="hide(item)" >{{item.name}}</RouterLink>
      <div :class="['layer',{'show':item.open}]" >
        <ul>
          <li v-for="i in item.children" :key="i.id">
            <RouterLink :to="`/category/sub/${i.id}`" @click="hide(item)" >
              <img
                :src="i.picture"
                alt=""
              />
              <p>{{i.name}}</p>
            </RouterLink>
          </li>
        </ul>
      </div>
    </li>

      </ul>
      <!-- <div class="search">
        <i class="iconfont icon-search"></i>
        <input type="text" placeholder="搜一搜">
      </div>
      <div class="cart">
        <a class="curr" href="#">
          <i class="iconfont icon-cart"></i><em>2</em>
        </a>
      </div> -->
    <!-- </div> -->
</template>
<script>
// 引入计算函数
import { computed } from 'vue'
// 引入注册store的方法
import { useStore } from 'vuex'

export default {
  name: 'AppHeaderNav',
  setup () {
    // 获取store
    const store = useStore()
    // 获取导航栏数据列表
    const list = computed(() => {
      // 返回在store获取到的列表
      return store.state.category.list
    })
    const show = (a) => {
      // 调用方法显示二级菜单
      store.commit('category/show', a)
    }
    const hide = (a) => {
      // 隐藏二级菜单
      store.commit('category/hide', a)
    }
    return { list, show, hide }
  }
}
</script>
<style lang="less" scoped>

  .navs {
    width: 820px;
    display: flex;
    justify-content: space-around;
    padding-left: 40px;
     position: relative;
  z-index: 998;
    >li {
      margin-right: 40px;
      width: 38px;
      text-align: center;
      >a {
        font-size: 16px;
        line-height: 32px;
        height: 32px;
        display: inline-block;
      }
      &:hover {
        >a {
          color: @xtxColor;
          border-bottom: 1px solid @xtxColor;
        }
      //    > .layer {
      //   height: 132px;
      //   opacity: 1;
      // }
      }
    }
  }
  .search {
    width: 170px;
    height: 32px;
    position: relative;
    border-bottom: 1px solid #e7e7e7;
    line-height: 32px;
    .icon-search {
      font-size: 18px;
      margin-left: 5px;
    }
    input {
      width: 140px;
      padding-left: 5px;
      color: #666;
    }
  }
  .cart {
    width: 50px;
    .curr {
      height: 32px;
      line-height: 32px;
      text-align: center;
      position: relative;
      display: block;
      .icon-cart{
        font-size: 22px;
      }
      em {
        font-style: normal;
        position: absolute;
        right: 0;
        top: 0;
        padding: 1px 6px;
        line-height: 1;
        background: @helpColor;
        color: #fff;
        font-size: 12px;
        border-radius: 10px;
        font-family: Arial;
      }
    }
  }
 .layer {
   &.show{
     height: 132px;
     opacity: 1;
   }
  width: 1240px;
  background-color: #fff;
  position: absolute;
  left: -200px;
  top: 56px;
  height: 0;
  overflow: hidden;
  opacity: 0;
  box-shadow: 0 0 5px #ccc;
  transition: all 0.2s 0.1s;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 70px;
    align-items: center;
    height: 132px;
    li {
      width: 110px;
      text-align: center;
      img {
        width: 60px;
        height: 60px;
      }
      p {
        padding-top: 10px;
      }
      &:hover {
        p {
          color: @xtxColor;
        }
      }
    }
  }
}

</style>
